package com.sunlee.admin.api.module.job.template;

import com.sunlee.admin.api.module.job.template.impl.CrawResourceCategoryFirstPpt;
import com.sunlee.admin.api.module.job.template.impl.CrawResourceFirstPpt;
import com.sunlee.admin.api.module.job.template.impl.GeneratePptFirstPpt;
import com.sunlee.common.core.exception.BusinessException;
import com.sunlee.common.core.utils.SpringContextUtils;
import com.sunlee.common.service.enums.ThirdFileSourceEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * 模板工厂
 *
 * @author sunlee
 */
public class TemplateFactory {

    public static final String THIRD_RESOURCE_CATEGORY_KEY = "THIRD_RESOURCE_CATEGORY_KEY_";
    public static final String THIRD_RESOURCE_KEY = "THIRD_RESOURCE_KEY_";
    public static final String THIRD_GENERATE_PPT_KEY = "THIRD_GENERATE_PPT_KEY_";

    private static final Map<String, Object> TEMPLATE_POOL_MAP = new HashMap<>(8);

    static {
        // 爬虫资源分类模板（CrawResourceCategoryTemplate）
        // 第一PPT
        TEMPLATE_POOL_MAP.put(THIRD_RESOURCE_CATEGORY_KEY + ThirdFileSourceEnum.FIRST_PPT.getCode(),
                SpringContextUtils.getBean(CrawResourceCategoryFirstPpt.class));

        // 爬虫资源模板（CrawResourceTemplate）
        // 第一PPT
        TEMPLATE_POOL_MAP.put(THIRD_RESOURCE_KEY + ThirdFileSourceEnum.FIRST_PPT.getCode(),
                SpringContextUtils.getBean(CrawResourceFirstPpt.class));

        // 三方资源构造模板（GeneratePptTemplate）
        // 第一PPT
        TEMPLATE_POOL_MAP.put(THIRD_GENERATE_PPT_KEY + ThirdFileSourceEnum.FIRST_PPT.getCode(),
                SpringContextUtils.getBean(GeneratePptFirstPpt.class));
    }

    public static CrawResourceCategoryTemplate getCrawResourceCategoryTemplateByCode(Integer code) {
        return getTemplate(THIRD_RESOURCE_CATEGORY_KEY + code);
    }

    public static CrawResourceTemplate getCrawResourceTemplateByCode(Integer code) {
        return getTemplate(THIRD_RESOURCE_KEY + code);
    }

    public static GeneratePptTemplate getGeneratePptTemplateByCode(Integer code) {
        return getTemplate(THIRD_GENERATE_PPT_KEY + code);
    }

    public static <T> T getTemplate(String key) {
        if (!TEMPLATE_POOL_MAP.containsKey(key)) {
            throw new BusinessException("获取模板异常");
        }
        return (T) TEMPLATE_POOL_MAP.get(key);
    }

}
